package top.leoseven.adminproject.view;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.ToastUtils;
import top.leoseven.adminproject.presenter.LoginPresenter;
import top.leoseven.adminproject.service.LoginService;

public class LoginAct extends BaseActivity implements LoginService, View.OnClickListener {
    private TextInputLayout username,password;
    private Button login_btn;
    private LoginPresenter loginPresenter;
    private ProgressDialog dialog;
    private static final String TAG=LoginAct.class.getName();
    @Override
    public void setLayout() {
        setContentView(R.layout.activity_login);
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            return;

        Window window = this.getWindow();

        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(0xff40e0d0);

        username= (TextInputLayout) findViewById(R.id.username);
        password= (TextInputLayout) findViewById(R.id.password);
        login_btn= (Button) findViewById(R.id.login_btn);
        login_btn.setOnClickListener(this);
        dialog=new ProgressDialog(this);
        dialog.setMessage("正在登陆......");
    }

    @Override
    public void initPresenter() {
        loginPresenter=new LoginPresenter(this,this);

    }

    @Override
    public void onSuccess() {
        dialog.dismiss();
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog!=null) {
            dialog.cancel();
        }
    }

    @Override
    public void onError(String error) {
        ToastUtils.toast(ToastUtils.JINGGAO,error,this,Toast.LENGTH_LONG);
        dialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        String p1=username.getEditText().getText().toString();
        String p2=password.getEditText().getText().toString();
        loginPresenter.Login(p1,p2);
        Log.e(TAG,"login");
        dialog.show();
    }
}
